<template>
  <div class="chart" ref="daliyChart"></div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  props: {
    chartData: {
      type: Array,
       default: () => []
    },
  },
  data() {
    return {
      xArr: [],
      yArr: [],
    }
  },
  watch: {
    'chartData': function (val) {
      this.xArr = val.map((item) => {
        return item.name.substring(5,10)
      })
      this.yArr = val.map((item) => {
        return Number(item.value)*3
      })
      this.initChartData()
    },
  },

  mounted() {
    // this.initChartData()
  },

  methods: {
    initChartData() {
      //   let xArr = []
      //   let yArr = []
      let xArr = this.xArr
      let yArr = this.yArr
      this.initDepartChart(xArr, yArr)
    },
    initDepartChart(xArr, yArr) {
      let myChart = echarts.init(this.$refs.daliyChart)
      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'none',
            label: {
              backgroundColor: '#6a7985',
            },
          },
        },
        grid: {
          left: '8%',
          right: '12%',
          top: '16%',
          bottom: '15%',
        },
        xAxis: {
          type: 'category',
          data: xArr,
          splitLine: {
            interval: 20,
          },
          axisLine: {
            lineStyle: {
              color: '#ccc',
            },
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            color: '#ccc',
            fontSize: 9,
          },
        },
        yAxis: {
          name: '访问量(次)',
          type: 'value',
          boundaryGap: ['0', '3%'],
          splitNumber: 3,
          axisLabel: {
            color: '#ccc',
            fontSize: 9,
          },
          splitLine: {
            lineStyle: {
              color: ['#ccc'],
              width: 1,
            },
          },
        },

        series: [
          {
            name: '访问量(次)',
            type: 'line',
            smooth: 0.6,
            symbol: 'circle',
            symbolSize: 5,
            itemStyle: {
              color: 'rgba(255, 255, 255, .85)',
            },
            lineStyle: {
              color: '#0A74F6',
              shadowColor: '#0A74F6', //透明的颜色
              opacity: 1, //透明度
              shadowBlur: 0, //阴影大小
              width: 2,
            },
            areaStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: 'rgba(10, 116, 246, 0.62)', // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: 'rgba(10, 116, 246, 0)', // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
            },
            data: yArr,
          },
        ],
      }
      if (!option.series[0].data) {
        this.lineExist = false
      }
      option && myChart.setOption(option)
    },
  },
}
</script>

<style lang="less" scoped>
.chart {
  height: 180px;
}
</style>